package doan.chuyennganh.util;

import java.util.Set;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class SharedPreferenceHelper {
	private Context context;
	private SharedPreferences pref;
	private Editor editor;
	public SharedPreferenceHelper(Context context){
		this.context = context;
		pref = this.context.getSharedPreferences(
				Constants.PREFERENCE_NAME, 0);
		editor = pref.edit();
	}
	private boolean isVarNull(){
		return (context == null || pref == null || editor == null);
	}
	public String getString(String key, String defaulValue) {
		if(isVarNull()) return defaulValue;
		String res = defaulValue;
		res = pref.getString(key, defaulValue);
		return res;
	}
	public int getInt(String key, int defaulValue) {
		if(isVarNull()) return defaulValue;
		int res = defaulValue;
		res = pref.getInt(key, defaulValue);
		return res;
	}
	public long getLong(String key, long defaulValue) {
		if(isVarNull()) return defaulValue;
		long res = defaulValue;
		res = pref.getLong(key, defaulValue);
		return res;
	}
	public boolean getBoolean(String key, boolean defaulValue) {
		if(isVarNull()) return defaulValue;
		boolean res = defaulValue;
		res = pref.getBoolean(key, defaulValue);
		return res;
	}
	@SuppressLint("NewApi")
	public Set<String> getStringSet(String key, Set<String> defaulValue) {
		if(isVarNull()) return defaulValue;
		Set<String> res = defaulValue;
		res = pref.getStringSet(key, defaulValue);
		return res;
	}
	public void putBoolean(String key, boolean value) {
		if(isVarNull()) return;
		editor.putBoolean(key, value);
		editor.commit();
	}
	public void putInt(String key, int value) {
		if(isVarNull()) return;
		editor.putInt(key, value);
		editor.commit();
	}
	public void putLong(String key, long value) {
		if(isVarNull()) return;
		editor.putLong(key, value);
		editor.commit();
	}
	public void putString(String key, String value) {
		if(isVarNull()) return;
		editor.putString(key, value);
		editor.commit();
	}
	@SuppressLint("NewApi")
	public void putStringSet(String key, Set<String> value) {
		if(isVarNull()) return;
		editor.putStringSet(key, value);
		editor.commit();
	}
}
